//定义js命名空间
Ext.namespace('FsCarInfo.FsGatelog');

// DateTime -- date , decimal -- float
// { name: 'Department.Name', mapping: 'Department.Name' },
// { name: 'Department'},
//通行记录Array
FsCarInfo.FsGatelog.BaseFieldsArray = new Array(
	{ name: 'Id', type: 'string' },
	{ name: 'LogId', type: 'string' },
	{ name: 'GateId', type: 'string' },
	{ name: 'IOType', type: 'string' },
	{ name: 'InArea.Name', mapping: 'InArea.Name' },
	{ name: 'OutArea.Name', mapping: 'OutArea.Name' },
	{ name: 'CreateUser', type: 'string' },
	{ name: 'Description', type: 'string' },
	{ name: 'ApprUserId', type: 'string' },
	{ name: 'ApprTime', type: 'DateTime' },
	{ name: 'ApprState', type: 'string' },
	{ name: 'CreateTime', type: 'DateTime' },
	{ name: 'ImgPath', type: 'string' },
	{ name: 'FsGatelog.CarNo', mapping: 'FsGatelog.CarNo' },
	{ name: 'FsGatelog.CardUid', mapping: 'FsGatelog.CardUid' },
	{ name: 'FsGatelog.Type', mapping: 'FsGatelog.Type' },
	{ name: 'FsGatelog.State', mapping: 'FsGatelog.State' },
	{ name: 'FsGateInfo.Name', mapping: 'FsGateInfo.Name' }
);

//ID卡返回信息
FsCarInfo.FsGatelog.IDCardFieldsArray = new Array(
	{ name: 'Id', type: 'string' },
	{ name: 'CardUid', type: 'string' },
	{ name: 'InfraIdcard.CardUid', mapping: 'InfraIdcard.CardUid' },
	{ name: 'CardInfo', type: 'string' },
	{ name: 'Type', type: 'string' },
	{ name: 'DeptId', type: 'string' },
	{ name: 'State', type: 'string' },
	{ name: 'ItemId', type: 'string' }
);

FsCarInfo.FsGatelog.MainLogFieldsArray = new Array(
	{ name: 'Id', type: 'string' },
	{ name: 'CardUid', type: 'string' },
	{ name: 'Type', type: 'string' },
	{ name: 'IoControlFlag', type: 'string' },
	{ name: 'CarNo', type: 'string' },
	{ name: 'State', type: 'string' },
	{ name: 'CreateTime', type: 'DateTime' },
	{ name: 'IsDelete', type: 'boolean' },
	{ name: 'Version', type: 'int' },
	{ name: 'Description', type: 'string' },
	{ name: 'InfraIdcard' },
	{ name: 'InfraIdcard.Type', mapping: 'InfraIdcard.Type' },
	{ name: 'LatestGateLog' }
);

//FsGatelog JsonReader
FsCarInfo.FsGatelog.JsonReader = Ext.extend(Infrastructure.BaseJsonReader, {
	constructor: function (cfg) {
		cfg = cfg || {};
		FsCarInfo.FsGatelog.JsonReader.superclass.constructor.call(this, Ext.apply({
			fields: FsCarInfo.FsGatelog.BaseFieldsArray
		}, cfg));
	}
});

//FsGatelog JsonStore
FsCarInfo.FsGatelog.JsonStore = Ext.extend(Infrastructure.BaseJsonStore, {
	constructor: function (cfg) {
		cfg = cfg || {};
		FsCarInfo.FsGatelog.JsonStore.superclass.constructor.call(this, Ext.apply({
			url: '/FsGatelogList/GetPlist',
			params: {
				start: 0,
				limit: ALL_PAGESIZE_SETTING
			},
			remoteSort: true,
			fields: FsCarInfo.FsGatelog.BaseFieldsArray,
			listeners: {
				'remove': function (thiz, record, index) {
					Ext.Ajax.request({
						url: '/FsGatelog/Delete?Id=' + record.data.Id,
						success: function (response, opts) {
							var result = Ext.util.JSON.decode(response.responseText);
							if (result.success == false) {
								JsHelper.ShowError(result.msg);
								this.insert(0, record);
							}
						}
					});
				}
			}
		}, cfg));
	}
});

//FsGatelog GridPanel
FsCarInfo.FsGatelog.GridPanel = Ext.extend(Ext.grid.GridPanel, {
	// User property
	//基本处理Url
	baseUrl: '/FsGatelog/',
	//提交保存Url
	formSubmitUrl: '/FsGatelog/SaveOrUpdate',
	//提交保存提示信息
	formSubmitMsg: '确定要保存吗?',
	//工具栏标签
	tbarMask: '0',
	//道闸ID
	gateId: '',

	// Property
	stripeRows: true,
	frame: true,
	border: true,
	layout: 'fit',
	loadMask: true,
	enableColumnMove: false,
	viewConfig: {
		autoFill: true
	},
	store: new FsCarInfo.FsGatelog.JsonStore(),

	//constructor
	initComponent: function () {
		//prepare

		var itemGrid = this;
		var tbarMask = this.tbarMask;
		var colMask = this.columnMask;
		var cmdAddEntity = this.onAddEntity;
		var cmdDeleteEntity = this.onDeleteEntity;
		var cmdLoadEntity = this.onLoadEntity;
		var cmdQueryList = this.onQueryList;
		//提交审批
		var cmdTbarCmd = function () {
			this.tbarSubmit(this.baseUrl + 'MethodName', '确定要提交吗?', '您的信息提交到系统..');
		};
		//set store params
		this.store.setBaseParam('state', this.itemState);
		this.store.setBaseParam('gateId', this.gateId);

		// The filter.
		var gridFilter = new Ext.ux.grid.GridFilters({
			encode: true,
			filters: [
				{ dataIndex: 'IOType', type: 'list', options: JsHelper.GetFilterListData(FsCarInfo.Params.Gate_IOTypeStore.data.items, 'Code', 'Name') },
				{ dataIndex: 'FsGatelog.Type', type: 'list', options: JsHelper.GetFilterListData(Infrastructure.Params.IDCardTypeStore.data.items, 'Code', 'Name') },
				{ dataIndex: 'FsGatelog.CarNo', type: 'string' },
				{ dataIndex: 'CreateUser', type: 'string' },
				{ dataIndex: 'CreateTime', type: 'date' },
				{ dataIndex: 'FsGatelog.EntTime', type: 'date' },
				{ dataIndex: 'FsGatelog.LevTime', type: 'date' },
				{ dataIndex: 'FsGatelog.State', type: 'list', options: JsHelper.GetFilterListData(FsCarInfo.Params.Gate_LogStateStore.data.items, 'Code', 'Name') },
				{ dataIndex: 'ApprState', type: 'list', options: JsHelper.GetFilterListData(FsCarInfo.Params.Gate_ApprStateStore.data.items, 'Code', 'Name') }
			]
		});

		// selModel
		var selModel = new Ext.grid.CheckboxSelectionModel({
			//header: '',
			//singleSelect: true,
			listeners: {
				'selectionchange': function (sm) {
					// 控制工具栏按钮是否可用
					itemGrid.btnApprove.setDisabled(sm.getCount() < 1);
					itemGrid.btnWriteOff.setDisabled(sm.getCount() < 1);
					itemGrid.btnReWrite.setDisabled(sm.getCount() < 1);

					// 根据状态控制
					var s = itemGrid.getSelectionModel().getSelections();
					for (var i = 0, r; r = s[i]; i++) {
						if (r.data.ApprState != '0') {
							itemGrid.btnApprove.setDisabled(true);
						}
						if (r.data["FsGatelog.State"] != '0') {
							itemGrid.btnWriteOff.setDisabled(true);
							itemGrid.btnReWrite.setDisabled(true);
						}
					}
				}
			}
		});

		// columnModel 对于不常需要显示的 hidden: true 
		var gridColumn = new Ext.grid.ColumnModel({
			columns: [
				new Ext.grid.RowNumberer(),
				selModel,
				{ header: "道闸", dataIndex: 'FsGateInfo.Name' },
				{ header: "出入", dataIndex: 'IOType',
					renderer: function (v) {
						return v == '' ? '' : FsCarInfo.Params.Gate_IOTypeStore.getById(v).get('Name');
					}
				},
				{ header: "类型", dataIndex: 'FsGatelog.Type',
					renderer: function (v) {
						return v == '' ? '' : Infrastructure.Params.IDCardTypeStore.getById(v).get('Name');
					}
				},
				{ header: "车牌", dataIndex: 'FsGatelog.CarNo' },
				{ header: "时间", dataIndex: 'CreateTime', xtype: 'datecolumn', format: 'Y-m-d H:i' },
				{ header: "保安", dataIndex: 'CreateUser' },
				{ header: "状态", dataIndex: 'FsGatelog.State',
					renderer: function (v) {
						return v == '' ? '' : FsCarInfo.Params.Gate_LogStateStore.getById(v).get('Name');
					}
				},
				{ header: "授权状态", dataIndex: 'ApprState',
					renderer: function (v) {
						return v == '' ? '' : FsCarInfo.Params.Gate_ApprStateStore.getById(v).get('Name');
					}
				}
			]
		});

		// PageBar
		var pagebBar = new Infrastructure.BasePagingToolbar({
			store: this.store,
			plugins: this.plugins
		});

		// 常用工具栏
		var toolBar = [{
			xtype: 'tbbutton',
			minWidth: 80,
			ref: '../btnApprove',
			text: '授权',
			disabled: true,
			hidden: tbarMask.charAt(0) != '1',
			icon: '/Content/icons/key.png',
			handler: function (e) {
				itemGrid.tbarSubmit('/FsGatelog/AccessApprove', '你确定要授权该车辆吗?', '授权成功,该车辆可离场。');
			},
			scope: itemGrid
		}, {
			hidden: tbarMask.charAt(0) != '1',
			xtype: 'tbseparator'
		}, {
			xtype: 'tbbutton',
			minWidth: 80,
			hidden: tbarMask.charAt(1) != '1',
			ref: '../btnToReset',
			text: '特殊处理',
			icon: '/Content/icons/cup_error.png',
			handler: function (e) {
				itemGrid.onResetEntity();
			},
			scope: itemGrid
		}, {
			hidden: tbarMask.charAt(1) != '1',
			xtype: 'tbseparator'
		}, {
			xtype: 'tbbutton',
			minWidth: 80,
			disabled: true,
			hidden: tbarMask.charAt(2) != '1',
			ref: '../btnWriteOff',
			text: '核销',
			icon: '/Content/icons/bug_delete.png',
			handler: function (e) {
				itemGrid.tbarSubmit('/FsGatelog/WriteOff', '你要核销选定的记录吗?', '核销成功!');
			},
			scope: itemGrid
		}, {
			hidden: tbarMask.charAt(2) != '1',
			xtype: 'tbseparator'
		}, {
			xtype: 'tbbutton',
			minWidth: 80,
			disabled: true,
			hidden: tbarMask.charAt(3) != '1',
			ref: '../btnReWrite',
			text: '重置',
			icon: '/Content/icons/arrow_undo.png',
			handler: function (e) {
				itemGrid.tbarSubmit('/FsGatelog/ReWrite', '你要重置选定的记录吗?', '重置成功!');
			},
			scope: itemGrid
		}, '-', {
			xtype: 'textfield',
			width: 150,
			id: 'q_query'
		}, {
			xtype: 'tbarqrybtn',
			handler: cmdQueryList,
			scope: itemGrid
		}, '-'];

		if (Ext.isDefined(this.tbar)) {
			toolBar = this.tbar;
		}

		Ext.apply(this, {
			plugins: [gridFilter],
			sm: selModel,
			cm: gridColumn,
			tbar: toolBar,
			bbar: pagebBar,
			listeners: {
				'rowdblclick': function (grid, rowIndex, e) {
					this.onLoadEntity(grid.store.getAt(rowIndex));
				}
			}
		});

		FsCarInfo.FsGatelog.GridPanel.superclass.initComponent.call(this);
	},

	/****************** 
	* user function 
	******************/

	// 添加
	onResetEntity: function () {
		var itemGrid = this;
		var itemEditForm = new FsCarInfo.FsGatelog.ResetForm({
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						ele.form.reset();
						var itemGridStore = itemGrid.store;
						itemGridStore.reload();
						JsHelper.ShowInfo('该ID卡已处理成功!');
					}
				}
			}
		});
		var itemEditWin = new Infrastructure.BaseWindow({
			title: '通行记录特殊处理',
			iconCls: 'door',
			width: 400,
			items: [itemEditForm]
		});
		itemEditWin.show();
	},

	// 读取信息显示
	onLoadEntity: function (selRecord) {
		var itemGrid = this;
		if (selRecord == null) {
			JsHelper.ShowError('请选择记录编辑..');
			return -1;
		}
		var selId = selRecord.data.Id;
		var itemEditForm = new FsCarInfo.FsGatelog.EditForm({
			Id: selId,
			dataRecord: selRecord,
			submitUrl: this.formSubmitUrl,
			submitMsg: this.formSubmitMsg,
			waitMsg: '数据加载中...',
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						itemGrid.store.reload();
						JsHelper.ShowInfo('信息更新成功!');
					}
				}
			}
		});
		var itemEditWin = new FsCarInfo.FsGatelog.EditWin({
			items: [itemEditForm]
		});
		itemEditWin.show();
		itemEditForm.form.loadRecord(selRecord);
	},

	// 删除
	onDeleteEntity: function () {
		var itemGrid = this;
		var itemGridStore = this.store;
		JsHelper.DelConfirm(function (btn) {
			if (btn == 'yes') {
				var s = itemGrid.getSelectionModel().getSelections();
				for (var i = 0, r; r = s[i]; i++) {
					itemGridStore.remove(r);
				}
			}
		});
	},

	// 查询
	onQueryList: function () {
		var itemGrid = this;
		var q_query = Ext.getCmp('q_query').getValue();
		itemGrid.store.load({
			params: {
				start: 0,
				limit: ALL_PAGESIZE_SETTING,
				q_query: q_query
			}
		})
	},

	// 提交选定记录..
	tbarSubmit: function (submitUrl, confirmMsg, successMsg) {
		var itemGrid = this;
		var selRecord = this.getSelectionModel().getSelected();
		if (selRecord == null) {
			JsHelper.ShowError('未选择记录.');
			return -1;
		}
		var selId = selRecord.data.Id;
		JsHelper.Confirm(confirmMsg, function (btn) {
			if (btn == 'yes') {
				var ids = [];
				var s = itemGrid.getSelectionModel().getSelections();
				for (var i = 0, r; r = s[i]; i++) {
					ids.push(r.data.Id);
				}
				//加载进度条
				Ext.MessageBox.show({
					msg: '正在提交数据到中心服务器...',
					progressText: '保存数据...',
					width: 300,
					wait: true,
					waitConfig: { interval: 1000 }
				});
				Ext.Ajax.request({
					url: submitUrl,
					params: {
						'Ids': ids.toString()
					},
					success: function (response, opts) {
						Ext.MessageBox.hide();
						var result = Ext.util.JSON.decode(response.responseText);
						if (result.success == false) {
							JsHelper.ShowError('操作失败:' + result.msg);
						} else {
							itemGrid.store.reload();
							Ext.Msg.alert('提示,操作成功', '成功。' + successMsg);
						}
					},
					failure: function (form, action) {
						Ext.MessageBox.hide();
						JsHelper.ShowActionError(action);
					}
				});
			}
		});
	}
});

//道闸登记的通行详细信息
FsCarInfo.FsGatelog.EditForm = Ext.extend(Infrastructure.BaseForm, {
	itemId: null,
	dataRecord: null,
	//labelAlign: 'top',
	labelWidth: 60,
	submitUrl: '/FsGatelog/SaveOrUpdate',
	submitMsg: '确定要保存吗?',

	initComponent: function () {
		var itemEditForm = this;
		var cmdUpdate = this.onSave;

		//Form第一列
		// 定义表单列 "FsGatelog": {
		//"Type
		var firstColumn = [{
			fieldLabel: "道闸",
			width: 150,
			readOnly: true,
			xtype: 'textfield',
			name: 'FsGateInfo.Name'
		}, {
			fieldLabel: "类型",
			width: 150,
			readOnly: true,
			xtype: 'infra_localcombo',
			store: Infrastructure.Params.IDCardTypeStore,
			name: 'FsGatelog.Type'
		}, {
			fieldLabel: "车牌",
			width: 150,
			maxLength: 10,
			readOnly: true,
			xtype: 'textfield',
			name: 'FsGatelog.CarNo'
		}, {
			fieldLabel: "出入",
			width: 150,
			readOnly: true,
			xtype: 'infra_localcombo',
			store: FsCarInfo.Params.Gate_IOTypeStore,
			name: 'IOType'
		}, {
			fieldLabel: "登记时间",
			width: 150,
			xtype: 'datefield',
			readOnly: true,
			format: 'Y-m-d H:i',
			name: 'CreateTime'
		}, {
			fieldLabel: "登记人",
			width: 150,
			readOnly: true,
			xtype: 'textfield',
			name: 'CreateUser'
		}, {
			fieldLabel: "授权时间",
			width: 150,
			xtype: 'datefield',
			readOnly: true,
			format: 'Y-m-d H:i',
			name: 'ApprTime'
		}, {
			fieldLabel: "授权人",
			width: 150,
			readOnly: true,
			xtype: 'textfield',
			name: 'ApprUserId'
		}];

		//Form第二列
		var secondColumn = [{
			fieldLabel: '图像',
			xtype: 'box', //或者xtype: 'component',
			id: 'ImgPath',
			autoEl: {
				tag: 'img',    //指定为img标签
				src: '../Content/icons/camera.gif'   //指定url路径
			}
		}];

		Ext.apply(this, {
			items: [{
				layout: 'column',
				// 注意 现在使用的是两列 要是三列或更多
				// 请改变下面的items
				// 并在上面定义每个列的组件集合
				items: [{
					width: '220',
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					items: firstColumn
				}, {
					columnWidth: .9,
					bodyStyle: 'padding:1px',
					layout: 'form',
					items: secondColumn
				}]
			}],
			reader: new FsCarInfo.FsGatelog.JsonReader()
		});

		FsCarInfo.FsGatelog.EditForm.superclass.initComponent.call(this);

		//定义保存后执行的事件
		this.addEvents('afterSave');
	},

	/****************** 
	* user function 
	******************/

	//更新保存信息
	onSave: function () {
		var itemForm = this;
		var id = this.Id;
		var saveUrl = this.submitUrl;
		if (itemForm.getForm().isValid()) {
			JsHelper.Confirm(this.submitMsg, function (btn) {
				if (btn != 'no') {
					itemForm.getForm().submit({
						waitMsg: '正在提交....',
						url: saveUrl,
						params: {
							'Id': (id == null) ? '' : id
						},
						success: function (form, action) {
							//返回结果后触发保存事件
							itemForm.fireEvent('afterSave', itemForm, true);
						},
						failure: function (form, action) {
							JsHelper.ShowError(action.result.msg);
						}
					});
				}
			});
		}
	}
});

//道闸登记的特殊处理
FsCarInfo.FsGatelog.ResetForm = Ext.extend(Infrastructure.BaseForm, {

	initComponent: function () {
		var itemEditForm = this;
		var cmdUpdate = this.onSave;

		//Form第一列
		// 定义表单列 "FsGatelog": {
		//"Type
		var firstColumn = [{
			fieldLabel: "ID卡",
			width: 150,
			xtype: 'infra_idcard_textfield',
			name: 'CardUId'
		}, {
			fieldLabel: "类型",
			width: 150,
			xtype: 'infra_localdictcombo',
			store: Infrastructure.Params.IDCardTypeStore,
			hiddenName: 'CardType'
		}, {
			fieldLabel: "车牌",
			width: 150,
			xtype: 'textfield',
			name: 'CarNo'
		}];

		//Form单列
		var singleColumn = [{
			xtype: 'textarea',
			fieldLabel: '描述',
			name: 'Description',
			height: 50,
			width: 200
		}];

		Ext.apply(this, {
			items: [{
				layout: 'column',
				// 注意 现在使用的是两列 要是三列或更多
				// 请改变下面的items
				// 并在上面定义每个列的组件集合
				items: [{
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					items: firstColumn
				}]
			}, singleColumn],
			buttons: [{
				xtype: 'btnsubmit',
				text: "核销",
				icon: "../Content/icons/bug_delete.png",
				handler: function (e) {
					itemEditForm.onSubmit('/FsGatelog/GateLogWriteOff', '确定要核销该ID卡的通行记录吗?核销后该ID卡的通行记录变为结束.');
				}
			}, {
				xtype: 'btnsubmit',
				text: "重置",
				icon: "../Content/icons/arrow_undo.png",
				handler: function (e) {
					itemEditForm.onSubmit('/FsGatelog/GateLogReWrite', '确定要重置该ID卡的通行记录吗?重置后该ID卡的原通行记录置为结束,并新增一个通行记录.');
				}
			}]
		});

		FsCarInfo.FsGatelog.EditForm.superclass.initComponent.call(this);

		//定义保存后执行的事件
		this.addEvents('afterSave');
	},

	/****************** 
	* user function 
	******************/

	//更新保存信息
	onSubmit: function (submitUrl, submitMsg) {
		var itemForm = this;
		var saveUrl = submitUrl;
		if (itemForm.getForm().isValid()) {
			JsHelper.Confirm(submitMsg, function (btn) {
				if (btn != 'no') {
					itemForm.getForm().submit({
						waitMsg: '正在提交....',
						url: saveUrl,
						success: function (form, action) {
							//返回结果后触发保存事件
							itemForm.fireEvent('afterSave', itemForm, true);
						},
						failure: function (form, action) {
							JsHelper.ShowError(action.result.msg);
						}
					});
				}
			});
		}
	}
});

//FsGatelog DetailWin
FsCarInfo.FsGatelog.EditWin = Ext.extend(Ext.Window, {
	title: '通行记录',
	iconCls: 'door_open',
	width: 650,
	resizable: false,
	constrain: true,
	autoHeight: true,
	modal: true,
	closeAction: 'close',
	plain: true,
	buttonAlign: 'center',

	//constructor
	initComponent: function () {
		var itemWin = this;
		Ext.apply(this, {
			buttons: [{
				xtype: 'btncancle',
				handler: function () {
					itemWin.destroy();
				}
			}]
		});
		FsCarInfo.FsGatelog.EditWin.superclass.initComponent.call(this);
	}
});

/**
*
*  道闸出入登记
*
**/
Ext.namespace('FsCarInfo.FsGateControl')

FsCarInfo.FsGateControl.ControlForm = Ext.extend(Infrastructure.BaseForm, {
	//user Prop
	gateId: '',
	//public prop
	height: 100,
	labelWidth: 50,
	reader: new Ext.data.JsonReader({
		root: 'data'
	}, FsCarInfo.FsGatelog.IDCardFieldsArray),

	// initComponent
	initComponent: function () {
		var form = this;

		var formReader = new Ext.data.JsonReader({
			root: 'data'
		}, FsCarInfo.FsGatelog.IDCardFieldsArray);

		Ext.apply(this, {
			items: [{
				layout: 'column',
				//reader: formReader,
				items: [{
					columnWidth: .5,
					layout: 'form',
					items: [{
						xtype: 'infra_idcard_textfield',
						fieldLabel: 'ID卡',
						name: 'CardUid',
						listeners: {
							specialkey: function (field, e) {
								if (e.getKey() == e.ENTER) {
									form.getCardInfo();
								}
							}
							/*,
							change: function (c) {
							form.getCardInfo(c.getValue());
							}*/
						},
						anchor: '95%'
					}, {
						xtype: 'textfield',
						fieldLabel: '车牌号',
						name: 'CarNo',
						listeners: {
							specialkey: function (field, e) {
								if (e.getKey() == e.ENTER) {
									form.getCardInfo();
								}
							}
						},
						anchor: '95%'
					}]
				}, {
					columnWidth: .5,
					layout: 'form',
					items: [{
						xtype: 'infra_localdictcombo',
						store: Infrastructure.Params.IDCardTypeStore,
						fieldLabel: '类型',
						readOnly: true,
						hiddenName: 'Type',
						anchor: '95%'
					}, {
						xtype: 'infra_localdictcombo',
						store: FsCarInfo.Params.Gate_CardStateStore,
						fieldLabel: '状态',
						readOnly: true,
						name: 'State',
						anchor: '95%'
					}]
				}]
			}],
			buttons: [{
				text: '进场',
				id: 'btn_CheckIn',
				cls: 'x-btn-text-icon',
				disabled: true,
				handler: function () {
					form.cardCheck('I');
				},
				icon: '../Content/icons/door_in.png'
			}, {
				text: '出场',
				id: 'btn_CheckOut',
				cls: 'x-btn-text-icon',
				disabled: true,
				handler: function () {
					form.cardCheck('O');
				},
				icon: '../Content/icons/door_out.png'
			}]
		});

		FsCarInfo.FsGateControl.ControlForm.superclass.initComponent.call(this);
	},

	/****************** 
	* user function 
	******************/

	// 读取IC卡信息
	getCardInfo: function (cardUId) {
		var baseForm = this;
		//baseForm.form.reset();
		var eleCardUidfocus = function () {
			baseForm.form.findField('CardUid').focus(true, 100);
		}
		var formValues = baseForm.form.getValues();
		// 自动补全 粤S
		var carNo = formValues.CarNo;
		if (carNo != "") {
			if (carNo.length == 5) {
				carNo = '粤S' + carNo;
			} else if (carNo.length == 6) {
				carNo = '粤' + carNo;
			}
			carNo = carNo.toUpperCase();
			baseForm.form.findField('CarNo').setValue(carNo);
		}
		if (formValues.CarNo == "" || formValues.CardUid == "") {
			return;
		}
		//读取信息
		//加载进度条
		Ext.MessageBox.show({
			msg: '正在读取ID卡信息...',
			progressText: '正在通讯...',
			width: 300,
			wait: true,
			waitConfig: { interval: 1000 }
		});
		Ext.Ajax.request({
			url: '/FsGatelog/GetLatestLogByIdCard',
			params: {
				'GateId': baseForm.gateId,
				'CardUId': formValues.CardUid,
				'CarNo': carNo
			},
			success: function (response, opts) {
				Ext.MessageBox.hide();
				var result = Ext.util.JSON.decode(response.responseText);
				if (result.success == false) {
					JsHelper.ShowError(result.msg, null, eleCardUidfocus);
				} else {
					var retRecord = baseForm.reader.readRecords(result)
					baseForm.getForm().loadRecord(retRecord.records[0]);
					//设置控件状态
					baseForm.buttons[0].setDisabled(result.data[0].IoControlFlag.indexOf('I') < '0');
					baseForm.buttons[1].setDisabled(result.data[0].IoControlFlag.indexOf('O') < '0');
					baseForm.form.findField('CardUid').focus(true);
				}
			},
			failure: function (form, action) {
				Ext.MessageBox.hide();
				JsHelper.ShowError('系统异常:' + action.result.msg);
			}
		});
	},

	// 车辆入场登记
	cardCheck: function (type) {
		//alert('车辆入场登记');
		var itemForm = this;
		if (itemForm.getForm().isValid()) {
			//TODO : 添加道闸控制
			JsHelper.Confirm('道闸控制异常..车辆是否已通过道闸?', function (btn) {
				if (btn != 'no') {
					itemForm.getForm().submit({
						waitMsg: '正在登记车辆出入信息....',
						url: '/FsGatelog/SaveOrUpdate',
						params: {
							'IOType': type,
							'GateId': itemForm.gateId
						},
						success: function (form, action) {
							//返回结果后触发保存事件
							itemForm.form.reset();
							itemForm.buttons[0].setDisabled(true);
							itemForm.buttons[1].setDisabled(true);
							JsHelper.ShowInfo('车辆出入信息登记成功!');
							itemForm.fireEvent('afterSave', itemForm, true);
						},
						failure: function (form, action) {
							JsHelper.ShowError(action.result.msg);
						}
					});
				}
			})
		}
	},

	// 车辆出场登记
	cardCheckOut: function () {
		alert('车辆出场登记');
	}
});

FsCarInfo.FsGateControl.ControlPanel = Ext.extend(Ext.Panel, {
	border: true,
	layout: 'border',

	// initComponent
	initComponent: function () {
		// 读取道闸ID
		//var gateId = '1fd0a38f-3a00-4839-b30a-95be510dcf33'; //大门道闸
		var gateId = '9f78fce0-d9ed-4853-ab02-b10520cfad78'; //授权道闸
		// 控制form
		var formReader = new Ext.data.JsonReader({
			root: 'data'
		}, FsCarInfo.FsGatelog.IDCardFieldsArray);

		// 清单
		var gateLogListGrid = new FsCarInfo.FsGatelog.GridPanel({
			gateId: gateId
		});

		// 输入区域
		var controlForm = new FsCarInfo.FsGateControl.ControlForm({
			gateId: gateId,
			reader: formReader,
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						gateLogListGrid.store.reload();
					}
				}
			}
		});

		// 实时监控显示区域
		var fieldsetVideo = new Ext.form.FieldSet({
			title: '出入口监控',
			//layout: 'column',
			labelAlign: 'top',
			labelWidth: 150,
			items: [{
				fieldLabel: '出口图像',
				xtype: 'box', //或者xtype: 'component',
				id: 'inpic',
				autoEl: {
					tag: 'img',    //指定为img标签
					src: '../Content/icons/camera.gif'   //指定url路径
				}
			}, {
				fieldLabel: '入口图像',
				xtype: 'box', //或者xtype: 'component',
				id: 'outpic',
				autoEl: {
					tag: 'img',    //指定为img标签
					src: '../Content/icons/camera.gif'   //指定url路径
				}
			}]
		});

		controlForm.region = 'north';
		gateLogListGrid.region = 'center';
		var cPanel = new Ext.Panel({
			border: true,
			layout: 'border',
			items: [controlForm, gateLogListGrid]
		});

		cPanel.region = 'center';
		fieldsetVideo.region = 'east';
		fieldsetVideo.width = '280';
		Ext.apply(this, {
			items: [cPanel, fieldsetVideo]
		});

		FsCarInfo.FsGateControl.ControlPanel.superclass.initComponent.call(this);

		gateLogListGrid.store.load({
			url: '/FsGatelogList/GetPlist',
			params: {
				start: 0,
				limit: ALL_PAGESIZE_SETTING
			}
		});
	}
});


